package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.EmployeeBean;
import com.project.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService service;

    @RequestMapping("login")
    public String login(String username, String password, HttpServletRequest req) {
        EmployeeBean employee = service.login(username, password);
        req.getSession().setAttribute("loginUser", employee);
        return "ok";
    }

    @RequestMapping("findByItem")
    public IPage<EmployeeBean> findByItem(Integer pageNO, String name, String dept) {
        return service.cutByItem(pageNO, name, dept);
    }

    @RequestMapping("findByIdArray")
    public List<EmployeeBean> findByIdArray(HttpServletRequest req) {
        Integer[] emIdArray =(Integer[])req.getSession().getAttribute("emIdArray");

        if (emIdArray.length<=0){
            return new ArrayList<>();
        }
        return service.findByIdArray(emIdArray);
    }

    @RequestMapping("addReceiverToEmail")
    public String addReceiverToEmail(Integer[] emIdArray,HttpServletRequest req){
        req.getSession().setAttribute("emIdArray",emIdArray);
        return "ok";
    }


    @RequestMapping("getLoginUser")
    public EmployeeBean getLoginUser(HttpServletRequest req) {
        return (EmployeeBean) req.getSession().getAttribute("loginUser");
    }
}
